package zhtt;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

/**
 * Created by zhtt on 2017/4/10.
 */
public class JunitTest_like {

    private Session session;

    @Before
    public void setUp(){
        session=HbnUtil.getSession();
    }

    @Test
    public void sqlLike(){
        try{
            session.beginTransaction();
            String sql="select * from zhtt0_student where name like :name";
            List<Student> list=session.createSQLQuery(sql)
                    .addEntity(Student.class)
                    .setParameter("name","%zh%")
                    .list();
            for(Student student:list){
                System.out.println(student);
            }
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();;
        }
    }

    @Test
    public void hqlList(){
        try{
            session.beginTransaction();
            String hql="from Student where name like :name";
            List<Student> list=session.createQuery(hql)
                    .setParameter("name","%zh%")
                    .list();
            for(Student student:list){
                System.out.println(student);
            }
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();;
        }
    }

    @Test
    public void qbcLike(){
        try{
            session.beginTransaction();
            List<Student> list=session.createCriteria(Student.class)
                    .add(Restrictions.like("name","%zh%"))
                    .list();
            for(Student student:list){
                System.out.println(student);
            }
            session.getTransaction().commit();
        }catch (Exception e){
            e.printStackTrace();;
        }
    }
}
